package atomic.longAdder;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author ZhuQi
 * @ClassName LongAdderDemo
 * @Date 2022/9/16 15:44
 * @Version 1.0
 * @Description 测试 LongAdder
 */
@Slf4j
public class LongAdderDemo {

    @SneakyThrows
    public static void main(String[] args) {
        LongAdder longAdder = new LongAdder();

        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 10000; j++) {
                    longAdder.increment();
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        log.info("longAdder:{}", longAdder.sum());

    }
}
